In [76]:
l = [1,2,3,4, 800, 40, 73, 8]
ll = [x*2 for x in l]
# ll = [2, 4, 6, 8, 1600, 80, 146, 16]
# list(enumerate(l))
{ k: k*2 for k in l }
ll.append("pippo")
zip(l, ll, ll, ll)
{ pippo: v for k,v,pippo in zip(l, ll, ll) }
x,y,z = 1,5,10
Out[76]:
In [83]:
import copy
x = [10, 3, 9]
y = copy.copy(x)
print("\n# y = copy.copy(x)")
print("x is y? {}".format(x is y))
print("x == y? {}".format(x == y))
print(id(x), id(y))
In [9]:
# Verifica l'identità di un oggetto
x = False
print("x is False? {}".format(x is False))
print(id(x), id(False))
x = 10
print("\nx is 10? {}".format(x is 10))
print(id(x), id(10))
x = [10, 3, 9]
y = x
print("\n# y = x")
print("x is y? {}".format(x is y))
print(id(x), id(y))
import copy
x = [10, 3, 9]
y = copy.copy(x)
print("\n# y = copy.copy(x)")
print("x is y? {}".format(x is y))
print("x == y? {}".format(x == y))
print(id(x), id(y))
In [2]:
import random
r = lambda : int(random.random()*3000)
# Uguale a
# def r():
# return int(random.random()*3000)
PEOPLE = [
{"name": "Luca", "city": "Fabriano", "salary": r()},
{"name": "Simone", "city": "Fabriano", "salary": r()},
{"name": "Elena", "city": "Mondavio", "salary":r()},
{"name": "Gianluca", "city": "Senigallia", "salary": r()},
{"name": "Monica", "city": "Roma", "salary": r()},
{"name": "Sonia", "city": "Bari", "salary": r()},
{"name": "Patrizia", "city": "Bari", "salary": r()},]
PEOPLE
Out[2]:
In [102]:
city_map = {}
for p in PEOPLE:
city = p["city"]
city_map[city] = city_map.get(city, [])
city_map[city].append(p)
import pprint
pprint.pprint(city_map)
In [98]:
class DictList(dict):
def get(self, k, default=None):
if k not in self:
self[k] = []
return super(DictList, self).get(k, default)
def __getitem__(self, k):
if k not in self:
# self[k] = []
super(DictList, self).__setitem__(k, [])
return super(DictList, self).__getitem__(k)
city_map = DictList()
l = city_map.get("pippo")
print(city_map)
In [101]:
city_map = DictList()
for p in PEOPLE:
city = p["city"]
# city_map[city] = city_map.get(city, [])
city_map.get(city).append(p)
import pprint
pprint.pprint(city_map)
In [109]:
# def get_name(x):
# return (x["city"], x["name"])
# PEOPLE.sort(key=get_name)
PEOPLE.sort(key=lambda x: (x["city"], x["name"]))
PEOPLE
Out[109]:
In [17]:
PEOPLE.sort(key=lambda x: x["salary"])
PEOPLE
Out[17]:
In [26]:
# v. gestionale/managers01/
In [119]:
class BaseManager(object):
def do_export(self, rows):
self.__privateattr = "PRIVATO"
print(u"Esporto qualcosa per te")
rv = self._internal_do_export(rows)
return rv
class FileManager(BaseManager):
def _internal_do_export(self, rows):
print(self.__privateattr)
print(u"ti esporto")
class ExtraFileManager(FileManager):
def _internal_do_export(self, rows):
print(u"ti esporto extra")
mymanager = FileManager()
mymanager.do_export([1,2])
mymanger.__privateattr
In [131]:
t = list((row[k] for k in ("name", "city", "salary")))
In [150]:
# **row
def wrappitto(f):
print("Prima di eseguirti...")
rv = f
print("...Dopo l'esecuzione")
return rv
@wrappitto
def hello(who="a chi?"):
print("Ciao {}".format(who))
hello()
In [151]:
def wrappitto(f):
def myfun(*args, **kwargs):
print("Prima di eseguirti...")
rv = f(*args, **kwargs)
print("...Dopo l'esecuzione")
return rv
return myfun
# hello = wrappitto(hello)
# hello = myfun
# hello(who="pippo")
@wrappitto
def hello(who="a chi?"):
print("Ciao {}".format(who))
hello()
In [185]:
def myprint(*args):
for i, arg in enumerate(args):
print("argomento {} = {}".format(i, arg))
def myprint_by_kw(*args, **kw):
l = kw.values() + list(args)
print("{} ".format(*args))
# myprint("ciao", "pippo", 1, {1:"bue"})
myprint_by_kw("ciao", name="pippo", city="Fabriano", salary=1)
print(1,2,3)
l = [1,2,3]
print(l)
In [186]:
"{name}".format(name="pippo")
Out[186]:
In [187]:
d = {"name": "pippo"}
"{name}".format(**d)
Out[187]:
In [190]:
l = [1,2]
"{} {}".format(*l)
Out[190]:
In [193]:
def myfun():
"""
la mia docstring
"""
print("ciao")
myfun.__doc__
myfun.__name__
Out[193]:
In [30]:
# v. managers03/base.py , decori.py e db.py
In [31]:
from functools import wraps
def profileme(f):
start = time.time()
@wraps
def wrapper(*args, **kw):
return f(*args, **kw)
stop = time.time()
print("Tempo {} secondi".format(stop-start))
return wrapper
In [ ]:
# v. modulo unittest
In [ ]:
# v. modulo threading